﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	TemplateFactory.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;

namespace Smart.Common.Templates
{
	/// <summary>
	/// Template factory
	/// </summary>
	public class TemplateFactory : ITemplateFactory
	{
		public SPServicesInstance Services { get; set; }

		public TemplateFactory(SPServicesInstance services)
		{
			Services = services;
		}

		/// <summary>
		/// Register template service
		/// </summary>
		public static void Register<TTemplateServiceType>(string serviceName)
			where TTemplateServiceType : class, ITemplateService, new()
		{
			SPServices.ServiceLocator.Register<ITemplateService, TTemplateServiceType>(serviceName);
		}

		/// <summary>
		/// Unregister template service
		/// </summary>
		public static void Unregister(string serviceName)
		{
			SPServices.ServiceLocator.Unregister<ITemplateService>(serviceName);
		}

		/// <summary>
		/// Returns template service
		/// </summary>
		protected TTemplateServiceType Get<TTemplateServiceType>(string serviceName)
			where TTemplateServiceType : class, ITemplateService
		{
			return Services.ServiceLocator.GetInstance<TTemplateServiceType>(serviceName);
		}

		/// <summary>
		/// Get service by serviceName
		/// </summary>
		public virtual ITemplateService GetService(string serviceName)
		{
			var service = Get<ITemplateService>(serviceName);

			if (service == null)
				throw new ArgumentOutOfRangeException("serviceName: " + serviceName);

			service.InitContext(serviceName);

			service.InitService();

			return service;
		}

		/// <summary>
		/// Create new template filter
		/// </summary>
		public virtual TemplateFilter CreateFilter(string serviceName)
		{
			var service = GetService(serviceName);

			return service.CreateFilter();
		}

		/// <summary>
		/// Returns list of templates
		/// </summary>
		public IList<ITemplate> GetTemplates(string serviceName, TemplateFilter filter)
		{
			var service = GetService(serviceName);

			return service.GetTemplates(filter);
		}

		/// <summary>
		/// Return list of templates
		/// </summary>
		public virtual IList<TTemplateType> GetTemplates<TTemplateType>(string serviceName, TemplateFilter filter)
			where TTemplateType : class, ITemplate
		{
			var service = GetService(serviceName);

			return service.GetTemplates<TTemplateType>(filter);
		}

		/// <summary>
		/// Format template
		/// </summary>
		public virtual bool Format(string serviceName, string templateId, string instanceId, IDictionary<string, object> values)
		{
			var service = GetService(serviceName);

			return service.Format(templateId, instanceId, values);
		}

		/// <summary>
		/// Format template
		/// </summary>
		public virtual ITemplateInstance Format(string serviceName, ITemplate template, string instanceId, IDictionary<string, object> values)
		{
			var service = GetService(serviceName);

			var instance = service.Source.CreateInstance(instanceId, template);

			return
				service.Format(template, instance, values)
					? instance
					: null;
		}

		/// <summary>
		/// Format template
		/// </summary>
		public virtual object FormatData(string serviceName, object templateData, IDictionary<string, object> values)
		{
			var service = GetService(serviceName);

			return service.FormatData(templateData, values);
		}

		/// <summary>
		/// Format template
		/// </summary>
		public virtual object FormatData(string serviceName, string templateId, IDictionary<string, object> values)
		{
			var service = GetService(serviceName);

			return service.FormatData(templateId, values);
		}
	}
}